package org.gwt.mosaic.samples.addressbook.client.util;

import java.util.ArrayList;
import java.util.List;

import org.gwt.mosaic.samples.addressbook.client.model.Person;

public class DataGenerator {

  public static final String[] firstNames = {
      "Arnold", "Bob", "Cathy", "Dennis", "Earl", "Frank", "George", "Hillary",
      "Irma", "John", "Kelly", "Michelle", "Natalie", "Oscar", "Paul", "Susan",
      "Todd"};

  public static final String[] lastNames = {
      "Anderson", "Billings", "Corper", "Donavon", "Elkins", "Fitzgerald",
      "Gaskins", "Haskins", "Iterby", "Johnson", "Kellickson", "Lee",
      "Peterson", "Richardson", "Sauxby", "Wilkinson", "York"};

  public static final String[] sports = {
      "baseball.com", "soccor.com", "football.com", "basketball.com",
      "hockey.com", "softball.com", "rugby.com", "waterpolo.com", "golf.com",
      "tennis.com"};

  /**
   * Generate a random {@link Person}.
   * 
   * @return a randomly generated person
   */
  public Person generateStudent() {
    Person student = new Person();
    student.setFirstName(nextValue(firstNames));
    student.setLastName(nextValue(lastNames));
    student.setEmail(student.getFirstName() + "." + student.getLastName() + "@"
        + nextValue(sports));
    return student;
  }

  /**
   * Generate a {@link List} of random {@link Person Persons}.
   * 
   * @param count the number of persons to generate
   * @return a list of randomly generated persons
   */
  public List<Person> generatePersons(int count) {
    List<Person> students = new ArrayList<Person>();
    for (int i = 0; i < count; i++) {
      students.add(generateStudent());
    }
    return students;
  }

  /**
   * Generate a random number.
   * 
   * @param max the max number to return, exclusive
   * @return a random int between 0 (inclusive) and max (exclusive)
   */
  protected int getRandomInt(int max) {
    return (int) (Math.random() * max);
  }

  /**
   * Get a random value from the specified array.
   * 
   * @param array the array to get the value from
   * @return a random value from the array
   */
  private String nextValue(String[] array) {
    return array[getRandomInt(array.length)];
  }

}